package com.basic.common;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FuncArray {
	public static Character[] convertFromStrToChar(String[] var){
    	if(var == null)
    		return null;
    	var = filterStrListBlank(var);
    	Character[] c = new Character[var.length];
    	for(int i=0; i<var.length; i++)
    		c[i] = var[i].charAt(0);
    	return c;
    }
    public static Long[] convertFromStrToLong(String[] var){
    	if(var == null)
    		return null;
    	var = filterStrListBlank(var);
    	Long[] c = new Long[var.length];
    	for(int i=0; i<var.length; i++)
    		c[i] = Long.parseLong(var[i]);
    	return c;
    }
    
    public static String linkArray(String[] array,String linkStr){
    	if(array == null)
    		return null;
    	StringBuilder sb = new StringBuilder();
    	for(String  one : array){
    		sb.append(one);
    		sb.append(linkStr); 
    	}
    	if(sb.length() > 0)
    		sb.delete(sb.length()-linkStr.length(), sb.length());
    	return sb.toString();
    }
    
    /**
     * 
     * @param array : "1","2","3"
     * @param linkStr : "-"
     * @return 1-2-3
     */
    public static String linkArray(List array,String linkStr){
    	
    	if(array == null)
    		return null;
    	StringBuilder sb = new StringBuilder();
    	for(Object  one : array){
    		sb.append(one);
    		sb.append(linkStr); 
    	}
    	if(sb.length() > 0)
    		sb.delete(sb.length()-linkStr.length(), sb.length());
    	return sb.toString();
    }
    public static String linkArray(Set array,String linkStr){
    	if(array == null)
    		return null;
    	StringBuilder sb = new StringBuilder();
    	for(Object  one : array){
    		sb.append(one);
    		sb.append(linkStr); 
    	}
    	if(sb.length() > 0)
    		sb.delete(sb.length()-linkStr.length(), sb.length());
    	return sb.toString();
    }
    public static String[] filterStrListBlank(String[] value) {

		ArrayList<String> al = new ArrayList<String>();
		for (String s : value) {
			if (s != null)
				if (!s.trim().equals(""))
					al.add(s);
		}
		String[] res = new String[al.size()];
		for (int i = 0; i < al.size(); i++) {
			res[i] = al.get(i);
		}
		return res;
	}
    
    public static Long[] removeDuplicate(Long[] var){
    	if(var == null) return null;
    	Set<Long>   set=new HashSet<Long>(); 
		for(Long item : var){
			set.add(item);
		}
		return (Long[]) set.toArray();
    }
}
